package com.springboot.orderechart.service.impl;

import com.springboot.orderechart.entity.Source;
import com.springboot.orderechart.mapper.SourceMapper;
import com.springboot.orderechart.service.ISourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.orderechart.util.Result;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-11
 */
@Service
public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> implements ISourceService {


    @Override
    public Result reload() {
        //清除数据
        this.remove(null);
        List<Source> sourceList=new ArrayList<>();
        //模拟生成1000条数据品牌
        String[] PHONE_NAMES = {"iPhone", "XiaoMi", "Huawei", "OPPO","ReadMi"};
        Random RANDOM = new Random();
        for (int i = 1; i <= 1000; i++) {
            Source order =new Source();
            order.setOrderid(i);
            order.setPhonename(PHONE_NAMES[RANDOM.nextInt(PHONE_NAMES.length)]);
            order.setPhoneid(phoneNameIndex(order.getPhonename()));
            order.setPrice(300 + (RANDOM.nextDouble() * 700));
            // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            // 增加1秒
            calendar.add(Calendar.SECOND, i);
            Date newDate = calendar.getTime();
            order.setConsumptiontime(newDate);
            sourceList.add(order);
        }
        if(sourceList!=null&&sourceList.size()>=1){
            this.saveBatch(sourceList);
        }
        return Result.reOk("生成成功");
    }

    @Override
    public Result getEchart() {
        List<Source> sourceList1=new ArrayList<>();
        //模拟生成1000条数据品牌
        String[] PHONE_NAMES = {"iPhone", "XiaoMi", "Huawei", "OPPO","ReadMi"};
        Random RANDOM = new Random();
        for (int i = 1; i <= 1; i++) {
            Source order =new Source();
            order.setPhonename(PHONE_NAMES[RANDOM.nextInt(PHONE_NAMES.length)]);
            order.setPhoneid(phoneNameIndex(order.getPhonename()));
            order.setPrice(10 + (RANDOM.nextDouble() * 700));
            // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            // 增加1秒
            calendar.add(Calendar.SECOND, i);
            Date newDate = calendar.getTime();
            order.setConsumptiontime(newDate);
            sourceList1.add(order);
            this.save(order);
        }
        Map<String,Object> map=new HashMap<>();
        List<String> phoneList=new ArrayList<>();
        List<Object> priceList=new ArrayList<>();
        List<Source> sourceList=this.baseMapper.getEchart();
        for (Source source : sourceList) {
            phoneList.add(source.getPhonename());
            priceList.add(source.getPrice());
        }
        map.put("phoneList",phoneList);
        map.put("priceList",priceList);
        return Result.reOk("查询成功",map);
    }

    private int phoneNameIndex(String name){
        if(name.equals("iPhone")){
            return 1;
        }
        if(name.equals("XiaoMi")){
            return 2;
        }
        if(name.equals("Huawei")){
            return 3;
        }
        if(name.equals("OPPO")){
            return 4;
        }
        if(name.equals("ReadMi")){
            return 5;
        }
        return 0;
    }




}
